﻿using System;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.mediator;
using SharpSqlAnalyzer.pp.mediator.type;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.select
{
	// Token: 0x0200022E RID: 558
	public class CTEProcessor : AbstractProcessor
	{
		// Token: 0x060012C4 RID: 4804 RVA: 0x0031A074 File Offset: 0x00318274
		public override void process(TParseTreeNode nodeArg)
		{
			TCustomSqlStatement tcustomSqlStatement = nodeArg as TCustomSqlStatement;
			if (tcustomSqlStatement.CteList == null || tcustomSqlStatement.CteList.size() == 0)
			{
				return;
			}
			bool parameter = this.getParameter<bool>(typeof(bool));
			KeywordAlignMediator keywordAlignMediator = MediatorFactory.newInstance<KeywordAlignMediator>(typeof(KeywordAlignMediator));
			TSourceToken tsourceToken = SourceTokenSearcher.backforwardSearch(tcustomSqlStatement.CteList.startToken, 5, "with");
			SourceTokenOperator.removeWhitespaceAndReturnFromStart(this.Option, tsourceToken.container, tsourceToken.posinlist + 1);
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(keywordAlignMediator.CurLevelIndentLen));
			SourceTokenOperator.addAfter(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
			for (int i = 0; i < tcustomSqlStatement.CteList.size(); i++)
			{
				TCTE cte = tcustomSqlStatement.CteList.getCTE(i);
				TSourceToken tsourceToken2 = SourceTokenSearcher.backforwardSearch(cte.PreparableStmt.startToken, 5, "as");
				SourceTokenOperator.removeWhitespaceAndReturnFormBeforeAndAfter(this.Option, tsourceToken2);
				if (parameter)
				{
					SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createReturnSourceToken());
					SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(SourceTokenOperator.curColumnNumberVT(tsourceToken) + "with".Length + 1));
				}
				else
				{
					SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(1));
				}
				SourceTokenOperator.addAfter(this.Option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(1));
				ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, cte.PreparableStmt, cte.PreparableStmt.startToken, cte.PreparableStmt.endToken);
			}
			TSourceToken targetToken;
			if (tcustomSqlStatement is TSelectSqlStatement)
			{
				targetToken = this.getFirstSelectStmt((TSelectSqlStatement)tcustomSqlStatement).SelectToken;
			}
			else
			{
				if (!(tcustomSqlStatement is TInsertSqlStatement))
				{
					return;
				}
				targetToken = ((TInsertSqlStatement)tcustomSqlStatement).InsertToken;
			}
			SourceTokenOperator.addBefore(this.Option, targetToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, targetToken, SourceTokenOperator.createWhitespaceSourceToken(SourceTokenOperator.curColumnNumberVT(tsourceToken) + this.Option.indentLen));
		}

		// Token: 0x060012C5 RID: 4805 RVA: 0x0031A268 File Offset: 0x00318468
		private TSelectSqlStatement getFirstSelectStmt(TSelectSqlStatement select)
		{
			if (select.SetOperatorType == ESetOperatorType.none)
			{
				return select;
			}
			return this.getFirstSelectStmt(select.LeftStmt);
		}
	}
}
